/*
 * Copyright (C) 2007 Eskil Bylund
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

using Gtk;

namespace DCSharp.Gui
{
	// TODO: Replace with SexyIconEntry from libsexy or a similar icon entry.
	public class SearchEntry : Entry
	{
		private bool isA11yTheme;

		public SearchEntry() : base()
		{
			WidthChars = 15;

			CheckA11y();
		}

		protected override void OnChanged()
		{
			UpdateStyle();
			base.OnChanged();
		}

		protected override void OnStyleSet(Style previousStyle)
		{
			// FIXME: This might not be the best place to check if the theme has
			// changed since this method will be called on each call to ModifyBase.
			CheckA11y();
			base.OnStyleSet(previousStyle);
		}

		private void CheckA11y()
		{
			Gtk.Settings settings = Gtk.Settings.GetForScreen(Screen);

			string themeName = settings.ThemeName; 
			if (themeName.StartsWith("HighContrast") ||
				themeName.StartsWith("LowContrast"))
			{
				isA11yTheme = true;
			}
			else
			{
				isA11yTheme = false;
			}
		}

		private void UpdateStyle()
		{
			if (!isA11yTheme && Text.Length > 0)
			{
				// Change to a yellow background color
				Gdk.Color background  = new Gdk.Color(247, 247, 190);
				ModifyBase(StateType.Normal, background);
			}
			else
			{
				// Reset to the default color 
				ModifyBase(StateType.Normal);
			}
			QueueDraw();
		}
	}
}
